import matplotlib.pyplot as plt
import csv
import operator
import datetime as dt
import pandas as pd
import numpy as np
import openpyxl
# Import
# ======
# essential libraries
import math
import random
from datetime import timedelta
# storing and anaysis
import numpy as np
import pandas as pd
# visualization
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objs as go
import plotly.figure_factory as ff
from plotly.subplots import make_subplots
import calmap
import folium
# color pallette
cnf, dth, rec, act = '#393e46', '#ff2e63', '#21bf73', '#fe9801'
# converter
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
# hide warnings
import warnings
warnings.filterwarnings('ignore')
df = pd.read_excel('C:\\Users\\TJ\\Desktop\\covid\\covid.xlsx')
pip install folium
pip install plotly
df.head()
df.info()
a=df['location']
temp = df.groupby('date')['total_cases', 'total_deaths'].sum().reset_index()
temp
temp = temp.melt(id_vars="date", value_vars=['total_cases', 'total_deaths'],
var_name='Case', value_name='Count')
temp.head()
fig = px.area(temp, x="date", y="Count", color='Case', height=600,
title='Cases over time', color_discrete_sequence = [rec, dth, act])
fig.update_layout(xaxis_rangeslider_visible=True)
fig.show()
# ========
# table
day_wise = df.groupby('date')['total_cases', 'total_deaths','new_cases'].sum().reset_index()
# number cases per 100 cases
day_wise['total_cases / total_deaths'] = round((day_wise['total_cases']/day_wise['total_deaths'])*100, 2)
# # no. of countries
day_wise['No. of countries'] = df[df['total_cases']!=0].groupby('date')['location'].unique().apply(len).values
day_wise['total_deaths / total_cases'] = round((day_wise['total_deaths']/day_wise['total_cases'])*100, 2)
# # fillna by 0
cols = ['total_cases / total_deaths']
day_wise[cols] = day_wise[cols].fillna(0)
day_wise.head(50)
fig_c = px.bar(day_wise, x="date", y="total_cases", color_discrete_sequence = [act])
fig_d = px.bar(day_wise, x="date", y="total_deaths", color_discrete_sequence = [dth])
fig = make_subplots(rows=1, cols=2, shared_xaxes=False, horizontal_spacing=0.1,
subplot_titles=('Confirmed cases', 'Deaths reported'))
fig.add_trace(fig_c['data'][0], row=1, col=1)
fig.add_trace(fig_d['data'][0], row=1, col=2)
fig.update_layout(height=480)
fig.show()
fig_1 = px.line(day_wise, x="date", y='total_deaths / total_cases', color_discrete_sequence = [dth])
fig = make_subplots(rows=1, cols=1, shared_xaxes=False,
subplot_titles=('치사율'))
fig.add_trace(fig_1['data'][0], row=1, col=1)
fig.update_layout(height=480)
fig.show()
fig_c = px.bar(day_wise, x="date", y="new_cases", color_discrete_sequence = [act])
fig = make_subplots(rows=1, cols=1, shared_xaxes=False, horizontal_spacing=0.5,
subplot_titles=("No. of new cases everyday"))
fig.add_trace(fig_c['data'][0], row=1, col=1)
fig.update_layout(height=480)
fig.show()
# Country wise
# ============
# getting latest values
country_wise = df[df['date']==max(df['date'])].reset_index(drop=True).drop('date', axis=1)
country_wise = country_wise.groupby('location')['total_cases','total_deaths','new_cases'].sum().reset_index()
country_wise.head(100)
country_wise.shape
fig_c = px.bar(country_wise.sort_values('total_cases').tail(30), x="total_cases", y="location",
text='total_cases', orientation='h', color_discrete_sequence = [act])
fig_d = px.bar(country_wise.sort_values('total_deaths').tail(30), x="total_deaths", y="location",
text='total_deaths', orientation='h', color_discrete_sequence = [dth])
fig = make_subplots(rows=1, cols=2, shared_xaxes=False, horizontal_spacing=0.14, vertical_spacing=0.08,
subplot_titles=('total_cases', 'total_deaths'))
fig.add_trace(fig_c['data'][0], row=1, col=1)
fig.add_trace(fig_d['data'][0], row=1, col=2)
fig_c = px.bar(country_wise.sort_values('new_cases').tail(15), x="new_cases", y="location",
text='new_cases', orientation='h', color_discrete_sequence = [act])
fig = make_subplots(rows=1, cols=1, shared_xaxes=False, horizontal_spacing=0.14, vertical_spacing=0.08,
subplot_titles=('new_cases'))
fig.add_trace(fig_c['data'][0], row=1, col=1)
fig = px.bar(df, x="date", y="total_cases", color='location', height=800,
title='total_cases', color_discrete_sequence = px.colors.cyclical.mygbm)
fig.show()
# =========================================
fig = px.bar(df, x="date", y="total_deaths", color='location', height=800,
title='total_deaths', color_discrete_sequence = px.colors.cyclical.mygbm)
fig.show()
# =========================================
fig = px.bar(df, x="date", y="new_cases", color='location', height=800,
title='new_cases', color_discrete_sequence = px.colors.cyclical.mygbm)
fig.show()